package com.baijiaoxi.common.model.base;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by shixj on 2018/3/13.
 */
@Slf4j
public class CoreException extends RuntimeException {

    protected Error error;

    protected Object data;

    protected Throwable e;

    public Error getError() {
        return error;
    }

    public Object getData() {
        return data;
    }

    public Throwable getE() {
        return e;
    }

    public CoreException setData(Object data) {
        this.data = data;
        return this;
    }

    public static CoreException create(Error error) {
        return new CoreException(error, error.msg(), null);
    }

    public static CoreException create(Error error, Object... args) {
        String msg = formatMsg(error, args);
        return new CoreException(error, msg, null);
    }

    public static CoreException create(Error error, Throwable e) {
        return new CoreException(error, error.msg(), e, null);
    }

    public static CoreException create(Error error, Throwable e, Object... args) {
        String msg = formatMsg(error, args);
        return new CoreException(error, msg, e, null);
    }

    protected CoreException(Error error, String msg, Object data) {
        super(msg);
        this.error = error;
        this.data = data;
        log.error("异常代码:{},异常信息:{},异常data:{}", error.value(), getMessage(), this.data, e);
    }

    protected CoreException(Error error, String msg, Throwable e, Object data) {
        super(msg, e);
        this.e = e;
        this.error = error;
        this.data = data;
        log.error("异常代码:{},异常信息:{},异常data:{}", error.value(), getMessage(), this.data, e);
    }

    public static String formatMsg(Error error, Object... args) {
        if (args == null || args.length == 0)
            return error.msg();
        String msgTemplate = error.msg();
        if (!msgTemplate.contains("%s")) {
            for (int i = 0; i < args.length; i++)
                msgTemplate += ":" + "%s" + (i == args.length - 1 ? "" : ",");
        }
        try {
            return String.format(msgTemplate, args);
        } catch (Exception e) {
            return error.msg();
        }
    }


    @Deprecated
    public CoreException(Throwable e, String message, Object data) {
        super(message);
        this.e = e;
        this.data = data;
    }

    @Deprecated
    public CoreException(String message, Object data) {
        super(message);
        this.data = data;
    }

    @Deprecated
    public CoreException(Throwable e, String message) {
        super(message);
        this.e = e;
    }

    @Deprecated
    public CoreException(String message) {
        super(message);
    }

    @Deprecated
    public CoreException(Error error) {
        super(error.msg());
        this.error = error;
    }

    @Deprecated
    public CoreException(Error error, Object... args) {
        super(formatMsg(error, args));
        this.error = error;
    }

    @Deprecated
    public CoreException(Object data, Error error, Object... args) {
        super(formatMsg(error, args));
        this.error = error;
        this.data = data;
    }
}
